<template>
  <div class="employees-container">
    <div class="app-container">
      <page-tools>
        <template #left>
          <div class="tips">
            <i class="el-icon-info" />
            <span>本月: 社保在缴 公积金在缴</span>
          </div>
        </template>
        <template #right>
          <el-button type="warning" size="small" @click="$router.push('/import')">excel导入</el-button>
          <el-button type="danger" size="small">excel导出</el-button>
          <el-button type="primary" size="small" @click="addEmployee">新增员工</el-button>
        </template>
      </page-tools>

      <el-card style="margin-top: 10px;">
        <el-table border :data="list">
          <el-table-column label="序号" type="index" />
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="工号" prop="workNumber" />
          <el-table-column label="聘用形式">
            <template #default="{row}">
              {{ formatEmployee(row.formOfEmployment) }}
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" width="180">
            <template #default="{row}">
              {{ formatTime(row.timeOfEntry) }}
            </template>
          </el-table-column>
          <el-table-column label="操作" fixed="right" width="200">
            <template #default="{row}">
              <el-button type="text" size="small" @click="getDetail(row.id)">查看</el-button>
              <el-button type="text" size="small">分配角色</el-button>
              <el-button type="text" size="small">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          layout="prev, pager, next"
          :page-size="params.size"
          :total="total"
          @current-change="changePage"
        />
      </el-card>
    </div>
    <add-employee :dialog-visible="dialogVisible" @close-dialog="closeDialog" />
  </div>
</template>
<script>
import { fetchEmployeeList } from '@/api/employee'
import dayjs from 'dayjs'
import AddEmployee from './components/add-employee.vue'
export default {
  name: 'EmployeeIndex',
  components: {
    AddEmployee
  },
  data() {
    return {
      list: [],
      params: {
        page: 1,
        size: 10
      },
      total: 0,
      dialogVisible: false
    }
  },
  mounted() {
    this.getEmployeeList()
  },
  methods: {
    async getEmployeeList() {
      const res = await fetchEmployeeList(this.params)
      this.list = res.rows
      this.total = res.total
    },
    formatEmployee(value) {
      const MAP = {
        1: '正式',
        2: '非正式',
        4: '其它'
      }
      return MAP[value]
    },
    formatTime(value) {
      return dayjs(value).format('YYYY-MM-DD')
    },
    changePage(page) {
      this.params.page = page
      this.getEmployeeList()
    },
    closeDialog() {
      this.dialogVisible = false
    },
    addEmployee() {
      this.dialogVisible = true
    },
    getDetail(id) {
      this.$router.push(
        {
          path: '/detail',
          query: {
            id
          }
        }
      )
    }
  }
}
</script>

<style>
</style>
